//  @ Project : GagalPanen
//  @ File Name : inventory.cpp
//  @ Date : 3/9/2011
//  @ Author : Denis
//
//


#include "inventory.hpp"
#include "inventoryconstants.hpp"

Inventory::Inventory(GameInterface* game) : game_(game), item_rep_(),
    money_(InventoryConstants::getInitialMoney())
    {
  /*Item* dum;
  for(int i = 0; i < 3; i++){
    item_rep_.push_back(new ItemSlot(dum));
  }*/
}

Inventory::Inventory(GameInterface* game, std::ifstream& is) :
    game_(game), item_rep_(), money_(0) {
  int n;
  is >> money_ >> n;

  for (int i = 0; i < n; ++i) {
    item_rep_.push_back(new ItemSlot(game_, is));
  }

}

void Inventory::save(std::ofstream& os) {
  os << getMoney() << " " << item_rep_.size() << std::endl;
  for (int i = 0; i < (int)item_rep_.size(); ++i) {
    item_rep_[i]->save(os);
  }
}

Inventory::~Inventory(){
  for(int i = 0; i < (int)item_rep_.size() ; i++)
    delete item_rep_[i];
}

bool Inventory::canAdd(Item* new_item) {
  bool exist_item = false;
  for(std::vector<ItemSlot*>::iterator it = item_rep_.begin(); it !=
          item_rep_.end(); ++it){
    if ((*it)->getItem()->getId() == new_item->getId() &&
            (*it)->getItem()->getType() == new_item->getType()){
      return true;
    }
  }

  if (!exist_item && (int)item_rep_.size() <
          InventoryConstants::getMaximumNumberOfItemSlots()){
    return true;
  } else if (!exist_item) {
    return false;
    // std::cout << "Inventory is Full" << std::endl;
  }
  return false;
}

void Inventory::add(Item* new_item) {

  bool exist_item = false;
  for(std::vector<ItemSlot*>::iterator it = item_rep_.begin(); it !=
          item_rep_.end(); ++it){
    if ((*it)->getItem()->getId() == new_item->getId() &&
            (*it)->getItem()->getType() == new_item->getType()){
      (*it)->add(new_item);
      exist_item = true;
    }
  }

  if (!exist_item && (int)item_rep_.size() <
          InventoryConstants::getMaximumNumberOfItemSlots()){
    item_rep_.push_back(new ItemSlot(game_, new_item));
  } else if (!exist_item) {
    assert(false);
    // std::cout << "Inventory is Full" << std::endl;
  }
  
  return;
}

int Inventory::getFilledSlotCount() {
  return (int)item_rep_.size();
}

Item* Inventory::getItemAt(int slot_number) {
  assert(slot_number >= 1 && slot_number <= (int)item_rep_.size());
  std::vector<ItemSlot*>::const_iterator it = item_rep_.begin()+(slot_number-1);
  return (*it)->getItem();
}

int Inventory::getMoney() {
  return money_;
}

int Inventory::numberOfItemsAtSlot(int slot_number){
  assert(slot_number >= 1 && slot_number <= (int)item_rep_.size());
  std::vector<ItemSlot*>::const_iterator it = item_rep_.begin()+slot_number-1;
  return (*it)->numberOfItems();
}

void Inventory::modifyMoney(int money) {
  money_ += money;
}

void Inventory::removeItem(Item* item) {
  for(std::vector<ItemSlot*>::iterator it = item_rep_.begin(); it !=
          item_rep_.end(); ++it) {
  if ((*it)->contains(item)){
     (*it)->removeItem(item);
     if ((*it)->numberOfItems() == 0) {
       delete *it;
       item_rep_.erase(it);
     }
     return;
    }
  }
  assert(false);
  return;
}

